package com.jerry.netty.http;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.QueryStringDecoder;

import java.util.List;
import java.util.Map;

/**
 * Created by Shirly on 2016/12/16.
 */
public class JerryRequest {
    private ChannelHandlerContext ctx;
    private HttpRequest request;

    public JerryRequest(ChannelHandlerContext ctx, HttpRequest request) {
      this.ctx = ctx;
        this.request = request;
    }

    //获取url
    public String geturl(){
        return request.getUri();
    }

    //获取method
    public String getMethod(){
        return  request.getMethod().name();
    }

   public Map<String,List<String>> getParamters(){
       //netty自带api查询url所带参数
       QueryStringDecoder decoder = new QueryStringDecoder(request.getUri());
       return  decoder.parameters();
   }

    public String getParamter(String name){
        Map<String,List<String>> params = getParamters();
        List<String> result = params.get(name.trim());
        if(result != null){
            return result.get(0);
        }else{
            return null;
        }
    }
}
